package com.psct.fragment;

import java.util.List;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.psct.R;
import com.psct.bean.UserAccount;
import com.psct.social.SocialAccount;
import com.psct.social.SocialManager;
import com.psct.social.SocialManager.OnBindListener;
import com.psct.social.UserAccountManager;
import com.psct.ui.PersonalActivity;

public class PersonCenterFragment extends SherlockFragment implements OnClickListener {

	private TextView tvAccount,tvGrade,tvRealName;
	
	private ListView listView;
	private MyAdapter adapter;
	private List<SocialAccount> list;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		((PersonalActivity) getActivity()).getSupportActionBar().setTitle("个人资料");
		list = SocialManager.getInstance(getActivity()).getAccountList();
		adapter = new MyAdapter();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_personal, container, false);
		listView = (ListView) view.findViewById(R.id.lv_social);
		listView.setAdapter(adapter);
		this.tvAccount=(TextView) view.findViewById(R.id.tv_account);
		this.tvGrade = (TextView) view.findViewById(R.id.tv_grade);
		this.tvRealName = (TextView) view.findViewById(R.id.tv_realname);
		UserAccount user = UserAccountManager.getInstance().getUser(getActivity());
		this.tvAccount.setText(user.userName);
		this.tvGrade.setText(user.grade);
		this.tvRealName.setText(user.realName);
		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_cancel:
			this.getActivity().finish();
			break;
		case R.id.btn_next:
			break;
		}

	}

	/**
	 * 适配器
	 * 
	 * @author zjingchuan
	 * 
	 */
	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getActivity()).inflate(R.layout.social_list_item,
						null);
			}
			// 用户头像，没有绑定的时候显示第三方的logo
			ImageView headView = (ImageView) convertView.findViewById(R.id.iv_head);
			// 第三方帐号的名字
			TextView tvTypeName = (TextView) convertView.findViewById(R.id.tv_type);
			// 个人昵称
			TextView tvNick = (TextView) convertView.findViewById(R.id.tv_nick);
			// 绑定解绑按钮
			TextView tvBind = (TextView) convertView.findViewById(R.id.tv_bind);
			if (list.get(position).isBind) {
				headView.setImageBitmap(BitmapFactory.decodeFile(list.get(position).iamge_file));
				tvTypeName.setText(list.get(position).typeName);
				tvNick.setText(list.get(position).name);
				tvBind.setText("解除绑定");
				tvBind.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						list.get(position).unBind(getActivity());
						SocialManager.getInstance(getActivity()).updateAccountList(list);
						adapter.notifyDataSetChanged();
					}

				});
			} else {
				headView.setImageResource(list.get(position).ico);
				tvTypeName.setText(list.get(position).typeName);
				tvNick.setText("未绑定");
				tvBind.setText("点击绑定");
				tvBind.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						list.get(position).bind(getActivity(), new OnBindListener() {

							@Override
							public void onBind() {
								SocialManager.getInstance(getActivity()).updateAccountList(list);
								getActivity().runOnUiThread(new Runnable() {
									public void run() {
										adapter.notifyDataSetChanged();
									}
								});
							}

						});
					}

				});
			}
			return convertView;
		}

	}
}
